package com.hrb.monkey.frament;


import android.content.ContentResolver;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.hrb.monkey.CustomApplication;
import com.hrb.monkey.R;
import com.hrb.monkey.activity.MainActivity;
import com.hrb.monkey.activity.zxing.ZxingCreatCodeActivity;
import com.hrb.monkey.activity.zxing.ZxingCustomActivity;
import com.hrb.monkey.ui.CustomTitleView;
import com.hrb.monkey.utils.ImageUtil;
import com.hrb.monkey.utils.ToastUtil;
import com.jzxiang.pickerview.TimePickerDialog;
import com.jzxiang.pickerview.data.Type;
import com.jzxiang.pickerview.listener.OnDateSetListener;
import com.uuzuche.lib_zxing.activity.CaptureActivity;
import com.uuzuche.lib_zxing.activity.CodeUtils;

import java.text.SimpleDateFormat;
import java.util.Date;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * A simple {@link Fragment} subclass.
 */
public class CustomViewFragment extends Fragment implements View.OnClickListener,OnDateSetListener {


    @BindView(R.id.custom_title_bar)
    CustomTitleView customTitleBar;
    Unbinder unbinder;
    @BindView(R.id.zxing_btn)
    Button zxingBtn;
    private final int REQUEST_CODE_ZXING = 0x01000;
    private final int REQUEST_CODE_ZXING_IMAGE = 0x01001;
    private final int REQUEST_CODE_ZXING_CUSTOMER = 0x01002;
    private final int REQUEST_CODE_ZXING_CREATE = 0x01003;
    @BindView(R.id.zxing_resolve_btn)
    Button zxingResolveBtn;
    @BindView(R.id.zxing_customer_btn)
    Button zxingCustomerBtn;
    @BindView(R.id.zxing_create_btn)
    Button zxingCreateBtn;

    TextView mTvTime;

    public CustomViewFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_custom_view, container, false);
        unbinder = ButterKnife.bind(this, view);
        customTitleBar.setTitleClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ToastUtil.show("sssss" + v.getId());
            }
        });
        zxingBtn.setOnClickListener(this);
        zxingCreateBtn.setOnClickListener(this);
        zxingCustomerBtn.setOnClickListener(this);
        zxingResolveBtn.setOnClickListener(this);
        view.findViewById(R.id.btn_all).setOnClickListener(this);
        view.findViewById(R.id.btn_year_month_day).setOnClickListener(this);
        view.findViewById(R.id.btn_year_month).setOnClickListener(this);
        view.findViewById(R.id.btn_month_day_hour_minute).setOnClickListener(this);
        view.findViewById(R.id.btn_hour_minute).setOnClickListener(this);
        init();
        mTvTime = (TextView) view.findViewById(R.id.tv_time);
        return view;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void onClick(View v) {
        //页面上的点击处理区域
        Intent intent;
        switch (v.getId()) {
            case R.id.zxing_btn:
                //普通扫描二维码
                intent = new Intent(CustomApplication.getContext(), CaptureActivity.class);
                startActivityForResult(intent, REQUEST_CODE_ZXING);
                break;
            case R.id.zxing_create_btn:
                //生成二维码
                intent = new Intent(CustomApplication.getContext(), ZxingCreatCodeActivity.class);
                startActivityForResult(intent, REQUEST_CODE_ZXING_CREATE);
                break;
            case R.id.zxing_customer_btn:
                //自定义页面的
                intent = new Intent(CustomApplication.getContext(), ZxingCustomActivity.class);
                startActivityForResult(intent, REQUEST_CODE_ZXING_CUSTOMER);
                break;
            case R.id.zxing_resolve_btn:
                //解析手机中的二维码
                intent = new Intent(Intent.ACTION_GET_CONTENT);
                intent.addCategory(Intent.CATEGORY_OPENABLE);
                intent.setType("image/*");
                startActivityForResult(intent,REQUEST_CODE_ZXING_IMAGE);
                break;
            case R.id.btn_all:
                mDialogAll.show(getActivity().getSupportFragmentManager(), "all");
                break;
            case R.id.btn_year_month:
                mDialogYearMonth.show(getActivity().getSupportFragmentManager(), "year_month");
                break;
            case R.id.btn_year_month_day:
                mDialogYearMonthDay.show(getActivity().getSupportFragmentManager(), "year_month_day");
                break;
            case R.id.btn_month_day_hour_minute:
                mDialogMonthDayHourMinute.show(getActivity().getSupportFragmentManager(), "month_day_hour_minute");
                break;
            case R.id.btn_hour_minute:
                mDialogHourMinute.show(getActivity().getSupportFragmentManager(), "hour_minute");
                break;
        }
    }



    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case REQUEST_CODE_ZXING:
                if (data != null) {
                    Bundle bundle = data.getExtras();
                    if (bundle == null) {
                        return;
                    }
                    if (bundle.getInt(CodeUtils.RESULT_TYPE) == CodeUtils.RESULT_SUCCESS) {
                        String result = bundle.getString(CodeUtils.RESULT_STRING);
                        ToastUtil.show("扫描结果为：" + result);
                    } else if (bundle.getInt(CodeUtils.RESULT_TYPE) == CodeUtils.RESULT_FAILED) {
                        ToastUtil.show("解码失败");
                    }
                }
                break;
            case REQUEST_CODE_ZXING_CREATE:
                break;
            case REQUEST_CODE_ZXING_IMAGE:
                if (data!=null){
                    Uri uri=data.getData();
                    ContentResolver cr=CustomApplication.getContext().getContentResolver();
                    try {
                        CodeUtils.analyzeBitmap(ImageUtil.getImageAbsolutePath(CustomApplication.getContext(), uri), new CodeUtils.AnalyzeCallback() {
                            @Override
                            public void onAnalyzeSuccess(Bitmap mBitmap, String result) {
                                cece(result);
                                Toast.makeText(CustomApplication.getContext(), "解析结果:" + result, Toast.LENGTH_LONG).show();
                            }

                            @Override
                            public void onAnalyzeFailed() {
                                Toast.makeText(CustomApplication.getContext(), "解析二维码失败", Toast.LENGTH_LONG).show();
                            }
                        });
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
                break;
            case REQUEST_CODE_ZXING_CUSTOMER:
                break;
        }
    }
    public void cece(String result){
        Bundle bundle1=new Bundle();
        bundle1.putString("zcode",result);
        MainActivity mainActivity=(MainActivity) getActivity();
        mainActivity.navView.setCheckedItem(R.id.nav_login);
        mainActivity.switchFragment(4,3,bundle1);
    }
    TimePickerDialog mDialogAll;
    TimePickerDialog mDialogYearMonth;
    TimePickerDialog mDialogYearMonthDay;
    TimePickerDialog mDialogMonthDayHourMinute;
    TimePickerDialog mDialogHourMinute;
    public void init(){

        long tenYears = 10L * 365 * 1000 * 60 * 60 * 24L;
        mDialogAll = new TimePickerDialog.Builder()
                .setCallBack(this)
                .setCancelStringId("取消")
                .setSureStringId("确定")
                .setTitleStringId("选择时间")
                .setYearText("年")
                .setMonthText("月")
                .setDayText("日")
                .setHourText("时")
                .setMinuteText("分")
                .setCyclic(false)
                .setMinMillseconds(System.currentTimeMillis()- tenYears)
                .setMaxMillseconds(System.currentTimeMillis() + tenYears)
                .setCurrentMillseconds(System.currentTimeMillis())
                .setThemeColor(getResources().getColor(R.color.timepicker_dialog_bg))
                .setType(Type.ALL)
                .setWheelItemTextNormalColor(getResources().getColor(R.color.timetimepicker_default_text_color))
                .setWheelItemTextSelectorColor(getResources().getColor(R.color.timepicker_toolbar_bg))
                .setWheelItemTextSize(12)
                .build();

        mDialogYearMonth = new TimePickerDialog.Builder()
                .setType(Type.YEAR_MONTH)
                .setThemeColor(getResources().getColor(R.color.colorPrimary))
                .setCallBack(this)
                .build();
        mDialogYearMonthDay = new TimePickerDialog.Builder()
                .setType(Type.YEAR_MONTH_DAY)
                .setCallBack(this)
                .build();
        mDialogMonthDayHourMinute = new TimePickerDialog.Builder()
                .setType(Type.MONTH_DAY_HOUR_MIN)
                .setCallBack(this)
                .build();
        mDialogHourMinute = new TimePickerDialog.Builder()
                .setType(Type.HOURS_MINS)
                .setCallBack(this)
                .build();
    }

    @Override
    public void onDateSet(TimePickerDialog timePickerView, long millseconds) {
        String text = getDateToString(millseconds);
        mTvTime.setText(text);
    }
    public String getDateToString(long time) {
        Date d = new Date(time);
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sf.format(d);
    }
}
